use std::sync::Arc;

use thunderdome::{Arena, Index};
use QcTools::utils::r#ref::Ref;

use crate::resources::ResourceManager;

use super::scene::Scene;

#[derive(Debug)]
pub struct SceneManager {
    currentScene: Option<Index>,
    scenes: Arena<Scene>,
}

impl SceneManager {
    pub fn new() -> Ref<Self> {
        let scene = Scene::new();
        let mut scenes = Arena::new();
        let current = scenes.insert(scene);

        Ref::new(Self {
            currentScene: Some(current),
            scenes,
        })
    }
    pub fn load_scene(&mut self, scene: Scene) -> Index {
        let current = self.scenes.insert(scene);
        self.currentScene = Some(current);
        current
    }

    pub fn load_scene_from_str(&mut self, scene: &str, res: Arc<ResourceManager>) {
        let current = self.scenes.insert(Scene::load(scene, res));
        self.currentScene = Some(current);
    }

    pub fn load_scene_from_index(&mut self, scene: Index) {
        self.currentScene = Some(scene);
    }

    pub fn preload_scene(&mut self, scene: Scene) -> Index {
        self.scenes.insert(scene)
    }

    pub fn get_current_scene(&self) -> Option<&Scene> {
        self.scenes.get(self.currentScene.unwrap())
    }
    pub fn get_current_scene_mut(&mut self) -> Option<&mut Scene> {
        self.scenes.get_mut(self.currentScene.unwrap())
    }
}
